package com.uenpay.oem.ui.reserver;


import android.text.TextUtils;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.socks.library.KLog;
import com.uenpay.im.ui.old.activity.wallet.ChargeByCardInvokItem;
import com.uenpay.im.ui.old.activity.wallet.WalletHandle;
import com.uenpay.oem.common.AppConstant;
import com.uenpay.oem.data.modelImpl.BankCardModel;
import com.uenpay.oem.data.modelImpl.TransactionModel;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.service.bean.TransBankCardList;
import com.uenpay.service.model.IBankCardModel;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;

/**
 * Author: liulou
 * Date: 2017/6/20.
 * desc:
 */

public class RechargePresenter implements RechargeContract.Presenter {

    private final TransactionModel mTransactionModel;
    private RechargeContract.View mRechargeView;

    private IBankCardModel mBankCardModel;
    private TransBankCardList<BankCardInfo> bankCardLists;
    private String tradeNo;
    private String wkPayNo;
    private HttpResultListener checkCodeResultListener = new HttpResultListener() {
        @Override
        public void onStart() {
            super.onStart();
            mRechargeView.showLoading();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mRechargeView.dismissLoading();
            mRechargeView.showToast("充值失败");
        }

        @Override
        public void onComplete() {
            super.onComplete();
            mRechargeView.dismissLoading();
            mRechargeView.goSuccess();
        }
    };

    public RechargePresenter(RechargeContract.View view) {
        mRechargeView = view;
        mBankCardModel = new BankCardModel();
        mTransactionModel = new TransactionModel();
    }


    @Override
    public void getTransBankCardList() {
        mBankCardModel.getTransBankList(AppConstant.TradeType.CHARHE, new HttpOnNextListener<TransBankCardList<BankCardInfo>>() {
            @Override
            public void onNext(TransBankCardList<BankCardInfo> bankCardList) {
                KLog.d("yang=====" + bankCardList.toString());
                mRechargeView.setTransBankCardList(bankCardList);
            }
        });
    }

    //发送验证码
    @Override
    public void sendSmsCode(String amount, String memo, final boolean isFirstSend) {

        mRechargeView.showLoading();

        WalletHandle.chargeByCard(amount, memo, new WalletHandle.IChargeByCardListener() {

            @Override
            public void onChargeResult(ChargeByCardInvokItem.ChargeByCardInvokItemResult mChargeByCardInvokItemResult) {
                mRechargeView.dismissLoading();

                if (mChargeByCardInvokItemResult != null) {
                    if (mChargeByCardInvokItemResult.status == HttpInvokeResultCode.RESULT_OK) {
                        KLog.d("recharge result:" + mChargeByCardInvokItemResult.rspCode + " " + mChargeByCardInvokItemResult.tradeNo + " " + mChargeByCardInvokItemResult.resMsg);
                        if (!TextUtils.isEmpty(mChargeByCardInvokItemResult.tradeNo) && !TextUtils.isEmpty(mChargeByCardInvokItemResult.wkPayNo)) {
                            if (!TextUtils.isEmpty(mChargeByCardInvokItemResult.resMsg) && !"null".equals(mChargeByCardInvokItemResult.resMsg)) {
                                mRechargeView.showToast(mChargeByCardInvokItemResult.resMsg);
                            } else {
                                tradeNo = mChargeByCardInvokItemResult.tradeNo;
                                wkPayNo = mChargeByCardInvokItemResult.wkPayNo;
                                if (isFirstSend) {
                                    //显示密码输入对话框
                                    mRechargeView.showInputSmsDialog();
                                } else {
                                    mRechargeView.showToast("已重新发送验证码");
                                }
                            }
                        }
                    } else if (!TextUtils.isEmpty(mChargeByCardInvokItemResult.msg)) {
                        mRechargeView.showToast(mChargeByCardInvokItemResult.msg);
                    }
                }
            }
        });
    }

    @Override
    public void recharge(String yzm) {
        mTransactionModel.checkCode(tradeNo, wkPayNo, yzm, checkCodeResultListener);
    }

    @Override
    public void chooseBank(int position) {
        mRechargeView.setRecommendBank(position);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {

    }
}
